home *** CD-ROM | disk | FTP | other *** search
/ 2,000 Greater & Lesser Mysteries / 2,000 Greater and Lesser Mysteries.iso / psychol / marble / fileview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-10  |  4.2 KB  |  170 lines

  1. /*---------------------------------------------------------*/
  2. /*                                                         */
  3. /*   Turbo Vision 1.0                                      */
  4. /*   Turbo Vision FileViewer Demo Support File             */
  5. /*   Copyright (c) 1991 by Borland International           */
  6. /*                                                         */
  7. /*---------------------------------------------------------*/
  8. #define Uses_MsgBox
  9. #define Uses_TKeys
  10. #define Uses_TScroller
  11. #define Uses_TDrawBuffer
  12. #define Uses_TRect
  13. #define Uses_TProgram
  14. #define Uses_TDeskTop
  15. #define Uses_TStreamableClass
  16. #include <tv.h>
  17. __link(RScroller)
  18. __link(RScrollBar)
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24.  
  25. #include <fstream.h>
  26.  
  27. #include "fileview.h"
  28.  
  29.  
  30. const char * const TFileViewer::name = "TFileViewer";
  31.  
  32. TFileViewer::TFileViewer( const TRect& bounds,
  33.                           TScrollBar *aHScrollBar,
  34.                           TScrollBar *aVScrollBar,
  35.                           const char *aFileName) :
  36.     TScroller( bounds, aHScrollBar, aVScrollBar )
  37. {
  38.     growMode = gfGrowHiX | gfGrowHiY;
  39.     isValid = True;
  40.     fileName = 0;
  41.     readFile( aFileName );
  42. }
  43.  
  44. TFileViewer::~TFileViewer()
  45. {
  46.      delete fileName;
  47.      destroy (fileLines);
  48. }
  49.  
  50. void TFileViewer::draw()
  51. {
  52.     char *p;
  53.  
  54.     ushort c =  getColor(0x0301);
  55.     for( int i = 0; i < size.y; i++ )
  56.         {
  57.         TDrawBuffer b;
  58.     b.moveChar( 0, ' ', c, size.x );
  59.  
  60.         if( delta.y + i < fileLines->getCount() )
  61.             {
  62.             char s[maxLineLength+1];
  63.             p = (char *)( fileLines->at(delta.y+i) );
  64.             if( p == 0 || strlen(p) < delta.x )
  65.                 s[0] = EOS;
  66.             else
  67.         {
  68.         strncpy( s, p+delta.x, size.x );
  69.         if( strlen( p + delta.x ) > size.x )
  70.                     s[size.x] = EOS;
  71.                 }
  72.             b.moveStr( 0, s, c );
  73.             }
  74.         writeBuf( 0, i, size.x, 1, b );
  75.         }
  76. }
  77.  
  78. void TFileViewer::scrollDraw()
  79. {
  80.     TScroller::scrollDraw();
  81.     draw();
  82. }
  83.  
  84. void TFileViewer::readFile( const char *fName )
  85. {
  86.     delete fileName;
  87.  
  88.     limit.x = 0;
  89.     fileName = newStr( fName );
  90.     fileLines = new TLineCollection(5, 5);
  91.     ifstream fileToView( fName );
  92.     if( !fileToView )
  93.         {
  94.         messageBox( "Invalid drive or directory", mfError | mfOKButton );
  95.         isValid = False;
  96.         }
  97.     else
  98.         {
  99.         char line[maxLineLength+1];
  100.         while( !fileToView.eof() && fileToView.get( line, sizeof line ) != 0 )
  101.             {
  102.             char c;
  103.             fileToView.get(c);      // grab trailing newline
  104.             limit.x = max( limit.x, strlen( line ) );
  105.             fileLines->insert( newStr( line ) );
  106.             }
  107.         isValid = True;
  108.         }
  109.     limit.y = fileLines->getCount();
  110. }
  111.  
  112. void TFileViewer::setState( ushort aState, Boolean enable )
  113. {
  114.     TScroller::setState( aState, enable );
  115.     if( enable && (aState & sfExposed) )
  116.         setLimit( limit.x, limit.y );
  117. }
  118.  
  119. Boolean TFileViewer::valid( ushort )
  120. {
  121.     return isValid;
  122. }
  123.  
  124. void *TFileViewer::read(ipstream& is)
  125. {
  126.     char *fName;
  127.  
  128.     TScroller::read(is);
  129.     fName = is.readString();
  130.     fileName = 0;
  131.     readFile(fName);
  132.     delete fName; 
  133.     return this;
  134. }
  135.  
  136. void TFileViewer::write(opstream& os)
  137. {
  138.     TScroller::write(os);
  139.     os.writeString(fileName);
  140. }
  141.  
  142. TStreamable *TFileViewer::build()
  143. {
  144.     return new TFileViewer( streamableInit );
  145. }
  146.  
  147.  
  148. TStreamableClass RFileView( TFileViewer::name,
  149.                             TFileViewer::build,
  150.                               __DELTA(TFileViewer)
  151.                           );
  152.  
  153.  
  154.  
  155.  
  156. TFileWindow::TFileWindow( const char *fileName ) :
  157.     TWindow( TProgram::deskTop->getExtent(), fileName, wnNoNumber ),
  158.     TWindowInit( &TFileWindow::initFrame )
  159. {
  160.     options |= (ofCenterX | ofCenterY | ofTileable);
  161.     TRect r( getExtent() );
  162.     r.grow(-1, -1);
  163.     insert(new TFileViewer( r,
  164.                             standardScrollBar(sbHorizontal | sbHandleKeyboard),
  165.                             standardScrollBar(sbVertical | sbHandleKeyboard),
  166.                             fileName) );
  167. }
  168.  
  169.  
  170.